using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using FrameWork.Source.Screen;

namespace FrameWork.Source.FrameWork
{
    class StateManager
    {
        public Game m_pGame;
        public SpriteBatch m_pSpriteHandler;
        private IGameState m_pCurrentState;
        private IGameState m_pNextState;
        static private GraphicsDeviceManager graphics = null;

        public StateManager(Game game , SpriteBatch pSpriteHandler)
        {
            m_pGame             = game;
            m_pSpriteHandler    = pSpriteHandler;
            m_pCurrentState     = null;
            m_pNextState        = null;
        }


        public static void initPortraitScreen(GraphicsDeviceManager graphics, bool isFullScreen)
        {
            if (graphics != null)
                StateManager.graphics = graphics;
            StateManager.graphics.PreferredBackBufferWidth = 480;
            StateManager.graphics.PreferredBackBufferHeight = 800;
            StateManager.graphics.SupportedOrientations = DisplayOrientation.Portrait;
            StateManager.graphics.IsFullScreen = isFullScreen;
            StateManager.graphics.ApplyChanges();
        }

        public static void swapToPortraitScreen()
        {
            initPortraitScreen(null, true);
        }

        public static void swapToLandScapeScreen()
        {
            initLandScapeScreen(null, true);
        }

        public static void initLandScapeScreen(GraphicsDeviceManager graphics, bool isFullScreen)
        {
            if (graphics != null)
                StateManager.graphics = graphics;
            StateManager.graphics.PreferredBackBufferWidth = 800;
            StateManager.graphics.PreferredBackBufferHeight = 480;
            StateManager.graphics.SupportedOrientations = DisplayOrientation.LandscapeRight | DisplayOrientation.LandscapeLeft;
            StateManager.graphics.IsFullScreen = isFullScreen;
            StateManager.graphics.ApplyChanges();
        }

        public static void initCustomScreen(GraphicsDeviceManager graphics, bool isFullScreen, int screenWidth, int screenHeight)
        {
            if (graphics != null)
                StateManager.graphics = graphics;
            StateManager.graphics.PreferredBackBufferWidth = screenWidth;
            StateManager.graphics.PreferredBackBufferHeight = screenHeight;
            StateManager.graphics.IsFullScreen = isFullScreen;
            StateManager.graphics.ApplyChanges();
        }

        public void init()
        {
            m_pNextState = m_pCurrentState = new SplashScreen(this);
            m_pCurrentState.init();
        }

        public void update(GameTime gameTime)
        {
            if (m_pCurrentState != m_pNextState)
            {
                m_pCurrentState.destroy();
                m_pCurrentState = m_pNextState;
                m_pCurrentState.init();
            }

            m_pCurrentState.update(gameTime);
        }

        public void render(GameTime gameTime)
        {
            m_pCurrentState.render(gameTime);
        }

        public void destroy()
        {
            m_pCurrentState.destroy();
        }

        public void setNextState(IGameState nextState)
        {
            m_pNextState = nextState;
        }
    }
}
